﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLHREntities;

namespace QLHuongRungDAO
{
    public class DonDatHangDAO
    {
        List<DonDatHang> listDDH = new List<DonDatHang>();
        private QLHuongRungEntities db = new QLHuongRungEntities();
        public DonDatHangDAO() { }
        public List<QLHREntities.DonDatHang> LayDanhSachDonDatHang()
        {
            listDDH = (from ddh in db.DonDatHangs
                       where ddh.TinhTrangGiao == 0
                         select ddh).ToList();
            return listDDH;
        }

        public List<CTDDH> LayChiTietDonDatHang(int maDDH)
        {
            var query = (from ctddh in db.ChiTietDonDatHangs
                        join cttd in db.ChiTietThucDons on ctddh.ChiTietThucDon equals cttd.MaCTTD
                        join nl in db.NguyenLieux on cttd.NguyenLieu equals nl.MaNL
                        join ma in db.MonAns on cttd.MonAn equals ma.MaMonAn
                        join ddh in db.DonDatHangs on ctddh.MaDDH equals ddh.MaDDH
                        where ctddh.MaDDH == maDDH && ddh.TinhTrangGiao == 0
                        select new { ctddh.MaDDH, nl.TenNguyenLieu, ma.TenMonAn, cttd.SoLuongDat }).ToList();
            List<CTDDH> list = new List<CTDDH>();
            foreach (var item in query)
            {
                CTDDH ctddh = new CTDDH();
                ctddh.MaDDH = (int)item.MaDDH;
                ctddh.TenNguyenLieu = item.TenNguyenLieu;
                ctddh.TenMonAn = item.TenMonAn;
                ctddh.SoLuongDat = (int)item.SoLuongDat;
                list.Add(ctddh);
            }
            return list;

        }

        public List<ChiTietThucDon> LayChiTietThucDonByMaDDH(int MaDDH_CapNhat)
        {
            List<ChiTietThucDon> list_cttd = new List<ChiTietThucDon>();
            list_cttd = (from ctddh in db.ChiTietDonDatHangs
                          join cttd in db.ChiTietThucDons on ctddh.ChiTietThucDon equals cttd.MaCTTD
                        where ctddh.MaDDH == MaDDH_CapNhat
                          select cttd).ToList();
            return list_cttd;
        }

        public void CapNhatTinhTrangGiao(int MaDDH_CapNhat)
        {
            DonDatHang donDatHang = (from ddh in db.DonDatHangs
                             where ddh.MaDDH == MaDDH_CapNhat
                            select ddh).First() ;
            donDatHang.TinhTrangGiao = 1;
            db.Entry(donDatHang).State = System.Data.EntityState.Modified;
            db.SaveChanges();
        }

        public void ThemDonDatHang(QLHREntities.DonDatHang donDatHang)
        {
            db.DonDatHangs.Add(donDatHang);
            db.SaveChanges();
        }

        public int LayMaDDH()
        {
            return (from ddh in db.DonDatHangs
                    orderby ddh.MaDDH descending
                    select new { ddh.MaDDH }).First().MaDDH;
        }

        public void ThemChiTIetDonDatHang(int size, List<ChiTietDonDatHang> list_ChiTietDonDatHang)
        {
            for (int i = 0; i < size; i++)
            {
                db.ChiTietDonDatHangs.Add(list_ChiTietDonDatHang[i]);
                db.SaveChanges();
            }
        }

        public void ThemChiTietThucDon(int size, List<ChiTietThucDon> list_ChiTietThucDon)
        {
            for (int i = 0; i < size; i++)
            {
                int maCTTD = list_ChiTietThucDon[i].MaCTTD;
                ChiTietThucDon chiTietThucDon = (from cttd in db.ChiTietThucDons
                                         where cttd.MaCTTD == maCTTD
                                         select cttd).First();
                chiTietThucDon.SoLuongDat = list_ChiTietThucDon[i].SoLuongDat;
                db.Entry(chiTietThucDon).State = System.Data.EntityState.Modified;
                db.SaveChanges();
            }            
        }
    }
}
